home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / kcl / akcl / akcl1615.lha / lsp / gprof_aix.hc < prev    next >
Text File  |  1990-08-13  |  6KB  |  256 lines

  1. #include <sys/types.h>
  2. #include <sys/limits.h>
  3. #include <mon.h>
  4. extern struct monglobal   _mondata;
  5. static struct desc {                           /*function descriptor fields*/
  6.   caddr_t begin;                  /*initial code address*/
  7.   caddr_t toc;                      /*table of contents address*/
  8.   caddr_t env;         /*environment pointer*/
  9.     } ;              /*function descriptor structure*/
  10. static struct desc *fd;         /*pointer to function descriptor*/
  11.  
  12. #include "../h/config.h"
  13. #include "../h/ext_sym.h"
  14. #define CF_FLAG (1 << 31) 
  15. extern char *kcl_self;
  16.  
  17. #define function_address(f)   (((struct desc *)(f))->begin)
  18.  
  19. mymonitor(low,high,x,leng)
  20.      int low,high;
  21.      object x;
  22. { if (0 == x) {monitor(0); return 0;}
  23.   monitor(low,high,x,leng);
  24. }
  25.  
  26. static int newmonstartup();
  27.  
  28. mymonstartup(low,high)
  29. caddr_t low,high;
  30. {
  31. /* static struct frag f[3];
  32.    
  33.  f[0].p_low = function_address(&__start);
  34.  f[0].p_high = function_address(&init_cmpwt);
  35.  f[1].p_low = low;
  36.  f[1].p_high = high;
  37.  f[2].p_low = 0;
  38.  f[3].p_high = 0;
  39.  newmonstartup(-1,f);
  40. */
  41.   _mondata.prof_type = _PROF_TYPE_IS_PG; 
  42.  return monstartup(low,high);
  43. }
  44.  
  45.  
  46. /*
  47. symbol table address + &__start == the real address
  48. [if the ld is done with -T0 ]
  49. */
  50.  
  51.  /*
  52. The format of symbol table entries.
  53. [144]    m   0x0001486c     .text     2  extern    void()          .Foo
  54. [145]    a2           0              60  188239     152            
  55.  
  56.  */
  57. /* the monstartup code in aix3.1 is broken: */
  58.  
  59.  
  60.  
  61.  
  62. static size1(f)
  63.      struct frag *f;
  64. { int range;
  65.   int fromsize;
  66.   int total = 0;
  67.   int tonum;
  68.   while (f->p_high)
  69.    {range = f->p_high - f->p_low;
  70.     fromsize = FROM_STG_SIZE(range);
  71.     tonum = TO_NUM_ELEMENTS(range);
  72.     if ( tonum < MINARCS ) tonum = MINARCS;
  73.     else
  74.       if ( tonum > TO_MAX-1 ) tonum = TO_MAX-1;
  75.     tonum = tonum * sizeof( struct tostruct );
  76.     total += fromsize + tonum + sizeof(struct gfctl);
  77.     f++;
  78.   }
  79.   return total;
  80. }
  81.  
  82. static int 
  83. newmonstartup(a,f)
  84.      struct frag *f;
  85. {struct prof *pb = malloc(3*sizeof(struct prof));
  86.  struct frag *ff =f;
  87.  int i = 0 ;
  88.  int nranges = 0;
  89.  int total = 0;
  90.  int range;
  91.  caddr_t buffer ;
  92.  int callcntsize;
  93.  bzero(pb,3*sizeof(struct prof));
  94.  
  95.  while(ff->p_high)
  96.    { 
  97.      pb[i].p_high = (caddr_t) ROUNDUP((int)f[i].p_high, INST_CNT_SIZE);
  98.      pb[i].p_low = (caddr_t) ROUNDDOWN((int)f[i].p_low, INST_CNT_SIZE);
  99.      range = pb[i].p_high - pb[i].p_low;
  100.      total += HIST_STG_SIZE(range);
  101.      ff++;
  102.      i++;
  103.    }
  104.   nranges = i;
  105.   callcntsize = size1(f);
  106.   total +=   callcntsize;
  107.  
  108.   buffer = (caddr_t) malloc(total);
  109.    
  110.  
  111.   _mondata.monstubuf = buffer;
  112.  
  113.   for (i=0; i < nranges; i++)
  114.     {pb[i].p_buff = (HISTCOUNTER *)buffer;
  115.      pb[i].p_scale = HIST_SCALE_1_TO_1;
  116.      pb[i].p_bufsize =  HIST_NUM_COUNTERS(pb[i].p_high-pb[i].p_low);
  117.      if (i == 0) {
  118.          pb[i].p_bufsize +=
  119.        (((callcntsize +  HIST_COUNTER_SIZE -1)/HIST_COUNTER_SIZE)
  120.         );
  121.        }
  122.      buffer += pb[i].p_bufsize * HIST_COUNTER_SIZE;
  123.  
  124.    }
  125.  monitor(1,1,pb,-1, callcntsize);
  126. }
  127.  
  128.  
  129.  
  130.  
  131.     
  132.     
  133.     
  134.     
  135.     
  136.  
  137.  
  138.  
  139.  
  140.  
  141. static char symname [200];
  142.  
  143. static
  144. sym_leng_and_copy(ux,copy)
  145.      unsigned int ux;
  146.      int copy;
  147. { char *from;
  148.  int leng=0;
  149.  if (ux & CF_FLAG)
  150.    {object x = (object) (ux & ~CF_FLAG);
  151.     if (x->cf.cf_name ==0)
  152.        from="ZUNDEF";
  153.      else {leng = x->cf.cf_name->s.s_fillp;
  154.        from = x->cf.cf_name->s.s_self;}}
  155.  else if (ux) 
  156.    { from= (char *)(ux);}
  157.  else {from="UNDEF";}
  158.  if (leng==0) leng=strlen(from);
  159.  if (leng >= sizeof(symname)) FEerror("Too long symbol",0);
  160.  if(copy) bcopy(from,symname,leng);
  161.  symname[leng]='0';
  162.  return leng;
  163. }
  164.     
  165.  
  166.  
  167. extern char * __start;
  168. extern char *core_end;
  169. extern int bzero();
  170.  
  171. static
  172. write_outsyms()
  173. {FILE *fdout,*fdin;
  174.  static struct syment sym;
  175.  static struct syment symaux;
  176.  struct filehdr Eheader;
  177.  struct aouthdr header;
  178.  struct scnhdr shdrs[15];
  179.  fdout= fopen("syms.out","w");
  180.  fdin=fopen(kcl_self,"r");
  181.  if (fdin == 0) FEerror("Can't find  akcl image");
  182.  fread(&Eheader,1,sizeof(Eheader),     fdin);
  183.  fread(&header,1,Eheader.f_opthdr,fdin);
  184.  fclose(fdin);
  185.  if (fdout == 0) FEerror("Can't open syms.out");
  186.  Eheader.f_nscns = 2;
  187.  Eheader.f_symptr = sizeof(Eheader) + sizeof(header)
  188.     +  Eheader.f_nscns*sizeof(struct scnhdr);
  189.  Eheader.f_nsyms = 2*(1+ combined_table.length);
  190.  bzero(&shdrs[0],10*sizeof(struct scnhdr));
  191.  bzero(&symaux,1*SYMESZ);
  192.  bzero(&sym,1*SYMESZ);
  193.  
  194.  header.tsize=0;
  195.  header.dsize=0;
  196.  header.bsize=0;
  197.  header.o_snentry=1;
  198.  header.o_sntext=1;
  199.  header.o_sndata=1;
  200.  header.o_sntoc=1;
  201.  header.o_snbss=1;
  202.  header.o_snloader=2;
  203.  
  204.  
  205.  fwrite(&Eheader,1,sizeof(Eheader),     fdout);
  206.  fwrite(&header,1,Eheader.f_opthdr,fdout);
  207.  fwrite(&shdrs[1],Eheader.f_nscns,sizeof(struct scnhdr),fdout);
  208.  fseek(fdout,Eheader.f_symptr,0);
  209.  
  210.  sym.n_scnum ==  header.o_sntext;
  211.  sym.n_sclass = 0x2 ;
  212.  sym.n_type = 0x20;
  213.  sym.n_numaux = 1;
  214.  printf("(&__start = 0x%x)",function_address(&__start));
  215.  {int i=0; int pos=4;
  216.   while (i < combined_table.length)
  217.     {      unsigned int adr = (unsigned int)(SYM_ADDRESS(combined_table,i));
  218.       /* printf("%d %d",i,SYM_STRING(combined_table,i));
  219.       fflush(stdout); */
  220.  
  221.       sym.n_offset = pos;
  222.       
  223.       sym.n_value=
  224.     (adr > 0x20000000 ?
  225.     (unsigned int) function_address(SYM_ADDRESS(combined_table,i))
  226.       -  0x10000e00
  227.      : adr  -  0x10000e00);
  228. /*      printf("\n %d %s 0x%x %x ",i,SYM_STRING(combined_table,i), adr, adr); */
  229.       fwrite(&sym,SYMESZ,1,fdout);
  230.       fwrite(&symaux,SYMESZ,1,fdout);
  231.       pos=pos+ sym_leng_and_copy(SYM_STRING(combined_table,i),1)+1;
  232. /*      printf("%s\n",symname); */
  233.       i++;
  234.     }
  235.  
  236.       sym.n_offset = pos;
  237.       sym.n_value=(int)core_end - (int) & __start;
  238.       fwrite(&sym,SYMESZ,1,fdout);
  239.       fwrite(&symaux,SYMESZ,1,fdout);
  240.       pos=pos+ strlen("_ENDSYM")+1;
  241.  
  242.  
  243.   fwrite(&pos,sizeof(pos),1,fdout);
  244.   printf("(at %d)",ftell(fdout));
  245.   for (i=0; i< combined_table.length ; i++)
  246.     {int leng=sym_leng_and_copy(SYM_STRING(combined_table,i),1);
  247.        fwrite(symname,leng,1,fdout);
  248.      putc(0,fdout);}
  249.    }
  250.  fwrite("_ENDSYM",8,1,fdout);
  251.  fclose(fdout);
  252. }
  253.  
  254.  
  255.  
  256.